package com.haha.extend.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.Long.library.R;


public class StateBitmapImageButton extends ImageView {
    private int maskColor = 0x40000000;
    private boolean initialized = false;

    public StateBitmapImageButton(Context context) {
        this(context,null);
    }

    public StateBitmapImageButton(Context context, AttributeSet attrs) {
        this(context, attrs,android.R.attr.imageButtonStyle);
    }
    public StateBitmapImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StateBitmapImageButton);
        maskColor = ta.getColor(R.styleable.StateBitmapImageButton_maskColour,0x40000000);
        ta.recycle();
        addStateEffect();
        initialized = true;
    }

    private void addStateEffect(){
        Drawable srcDrawable = getDrawable();
        Drawable bgDrawable = getBackground();
        if(srcDrawable != null && srcDrawable instanceof BitmapDrawable){
            setImageDrawable(createStateDrawable((BitmapDrawable) srcDrawable,maskColor));
        } else if(bgDrawable != null && bgDrawable instanceof BitmapDrawable){
            setBackgroundDrawable(createStateDrawable((BitmapDrawable) bgDrawable,maskColor));
        }
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        if(initialized) addStateEffect();
    }

    @Override
    public void setBackgroundDrawable(Drawable background) {
        super.setBackgroundDrawable(background);
        if(initialized) addStateEffect();
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        if(initialized) addStateEffect();
    }

    @Override
    public void setBackgroundResource(int resid) {
        super.setBackgroundResource(resid);
        if(initialized) addStateEffect();
    }

    @Override
    public void setBackground(Drawable background) {
        super.setBackground(background);
        if(initialized) addStateEffect();
    }

    private StateListDrawable createStateDrawable(BitmapDrawable drawable, int maskColor){
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(View.PRESSED_ENABLED_STATE_SET, createPressedDrawable(drawable,maskColor));
        stateListDrawable.addState(View.EMPTY_STATE_SET,drawable);
        return stateListDrawable;
    }
    private Drawable createPressedDrawable(BitmapDrawable drawable,int maskColor){
        Bitmap bitmap = drawable.getBitmap().copy(Bitmap.Config.ARGB_8888,true);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int len = width * height;
        int[] pixels = new int[len];
        bitmap.getPixels(pixels,0,width,0,0,width,height);
        for(int i = 0; i < len; i++) if(pixels[i] != 0x00000000) pixels[i] = maskColor;
        new Canvas(bitmap).drawBitmap(Bitmap.createBitmap(pixels,width,height, Bitmap.Config.ARGB_8888),0,0,null);
        return new BitmapDrawable(bitmap);
    }



}
